package cn.frontop.funswitch.controller;

import cn.frontop.funswitch.common.Result;
import cn.frontop.funswitch.entity.ApiPathEntity;
import cn.frontop.funswitch.entity.DatasourceEntity;
import cn.frontop.funswitch.service.DatasourceService;
import cn.frontop.funswitch.util.CommonUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * 数据源管理
 *
 * @author Yun
 * @email Yun@frontop.cn
 * @date 2022-10-19 15:41:15
 */
@RestController
@RequestMapping("/datasource")
@Api(tags = "数据源管理")
public class DatasourceController {
    @Autowired
    private DatasourceService datasourceService;

    /**
     * 查看数据源管理列表
     *
     * @param pageNo   当前页码
     * @param pageSize 每页显示条数
     * @return 结果集
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation("查看数据源管理列表")
    public Result<?> queryApiPathList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                      @RequestParam(name = "projectId", defaultValue = "1") Integer projectId) {
        return CommonUtil.entityPageList(datasourceService, pageNo, pageSize, new QueryWrapper<DatasourceEntity>().lambda().eq(DatasourceEntity::getProjectId, projectId).orderByDesc(DatasourceEntity::getCreatedTime));

    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("查询数据源信息")
    public Result<DatasourceEntity> info(@PathVariable("id") Integer id) {
        DatasourceEntity datasource = datasourceService.getById(id);
        return Result.ok(datasource);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("保存数据源")
    public Result<String> save(@RequestBody DatasourceEntity datasource) {
        datasourceService.save(datasource);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("更新数据源")
    public Result<String> update(@RequestBody DatasourceEntity datasource) {
        datasource.setUpdatedTime(new Date());
        datasourceService.updateById(datasource);
        return Result.ok();
    }

    /**
     * 删除
     */
    @GetMapping("/delete/{id}")
    @ApiOperation("删除数据源")
    public Result<String> delete(@PathVariable("id") Integer id) {
        datasourceService.removeById(id);
        return Result.ok();
    }

}
